package com.jac.qpsc;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.example.mylibrarybase.bean.PrePaymentBean;
import com.example.mylibrarybase.net.netWork;
import com.example.mylibrarybase.utils.LogUtils;
import com.example.mylibrarybase.utils.UtilsToast;
import com.jac.qpsc.zhifubao.PayDemoActivity;
import com.lzy.okgo.cache.CacheMode;
import com.tencent.mm.sdk.modelpay.PayReq;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import java.util.HashMap;

public class MainActivity extends Activity {

    private TextView tv_buy_weixin;
    private TextView tv_buy_zhifubao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv_buy_weixin = (TextView) findViewById(R.id.tv_buy_weixin);
        tv_buy_zhifubao = (TextView) findViewById(R.id.tv_buy_zhifubao);

        tv_buy_weixin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UtilsToast.showToast(MainActivity.this, "我点了微信购买");
                HashMap<String, String> params = new HashMap<String, String>();
                //String token = Share.getToken(MainActivity.this);
                params.put("phone", "18603663029");
                params.put("password", "123456");
                netWork.goPost(MainActivity.this, netWork.LOGIN_URL,params , "0", CacheMode.NO_CACHE, new netWork.CallBack() {
                    @Override
                    public void onSuccess(JSONObject jsonObject) {
                        LogUtils.i("token"+jsonObject);
                        String token = jsonObject.getString("token");
                        pay(token);
                    }

                    @Override
                    public void onError(String error) {

                    }
                });

            }
        });
        tv_buy_zhifubao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UtilsToast.showToast(MainActivity.this, "我点了支付宝购买");
                startActivity(new Intent(MainActivity.this, PayDemoActivity.class));
            }
        });

    }

    private void pay(String token) {
        HashMap<String, String> params = new HashMap<String, String>();
        //String token = Share.getToken(MainActivity.this);
        params.put("token", token);
        params.put("order_no", "1477455128150553");
        netWork.goPost(MainActivity.this, netWork.PAYMENT_REQUEST,params , "0", CacheMode.NO_CACHE, new netWork.CallBack() {
            @Override
            public void onSuccess(JSONObject jsonObject) {
                PrePaymentBean prePaymentBean = jsonObject.getObject("data", PrePaymentBean.class);
                LogUtils.i("PrePaymentBean预支付返回来的内容" + prePaymentBean.toString());
                // 通过WXAPIFactory工厂，获取IWXAPI的实例
                IWXAPI api = WXAPIFactory.createWXAPI(MainActivity.this, prePaymentBean.appid, true);
                api.registerApp(prePaymentBean.appid);
                //等待回调
                if (!api.isWXAppInstalled()) {
                    Toast.makeText(MainActivity.this, "您还未安装微信客户端", Toast.LENGTH_SHORT).show();
                    return;
                }
                // 将该app注册到微信
                PayReq req = new PayReq();
                req.appId = prePaymentBean.appid;
                req.partnerId = prePaymentBean.partnerid;
                req.prepayId = prePaymentBean.prepayid;
                req.packageValue = prePaymentBean.packageValue;
                req.nonceStr = prePaymentBean.noncestr;
                req.timeStamp = prePaymentBean.timestamp;
                req.sign = prePaymentBean.sign;
                api.sendReq(req);
            }

            @Override
            public void onError(String error) {

            }
        });

    }
}
